unit HP;

interface

uses
  Database, EnemySetup,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Gauges, Menus, ComCtrls, DBCtrls;

type
  TFormHP = class(TForm)
    Memo1: TMemo;
    LabelPaste: TLabel;
    Timer: TTimer;
    PanelMain: TPanel;
    igp22: TImage;
    igp12: TImage;
    isp2: TImage;
    ihp2: TImage;
    GaugeGP2: TGauge;
    GaugeSP: TGauge;
    GaugeHP: TGauge;
    GaugeGP1: TGauge;
    ImageSPm: TImage;
    ImageGP1: TImage;
    ImageGP2: TImage;
    Image3: TImage;
    Image4: TImage;
    ImageHPm: TImage;
    Image5: TImage;
    Image2: TImage;
    LabelPhase: TLabel;
    BtnNext: TBitBtn;
    BtnSkip: TBitBtn;
    Label6: TLabel;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    PanelReady: TPanel;
    BtnReady: TBitBtn;
    LabelHP: TLabel;
    LabelSP: TLabel;
    LabelGP2: TLabel;
    LabelGP1: TLabel;
    EditPaste: TEdit;
    Bevel2: TBevel;
    BtnReset: TBitBtn;
    s1: TImage;
    s2: TImage;
    s3: TImage;
    s4: TImage;
    BtnDummy: TBitBtn;
    BtnMove: TBitBtn;
    EditEvent: TEdit;
    BtnUndoDummy: TBitBtn;
    GaugeEnemy: TGauge;
    ienemy2: TImage;
    ImageEnemy: TImage;
    Image6: TImage;
    LabelEnemy: TLabel;
    s5: TImage;
    Label7: TLabel;
    EditVars: TEdit;
    Memo2: TMemo;
    PanelCover: TPanel;
    EditEnemy: TEdit;
    Label1: TLabel;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    MenuInvisible: TMenuItem;
    MenuESC: TMenuItem;
    MenuHelp: TMenuItem;
    Memo3: TMemo;
    BtnPaste: TBitBtn;
    BtnNo: TBitBtn;
    EditText: TDBRichEdit;
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure BtnNextClick(Sender: TObject);
    procedure BtnResetClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure EditTextKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure BtnSkipClick(Sender: TObject);
    procedure BtnReadyClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EditPasteKeyPress(Sender: TObject; var Key: Char);
    procedure EditPasteChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnDummyClick(Sender: TObject);
    procedure EditEventKeyPress(Sender: TObject; var Key: Char);
    procedure BtnUndoDummyClick(Sender: TObject);
    procedure BtnMoveClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnPasteClick(Sender: TObject);
    procedure BtnNoClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdatePhase;
    procedure SetupEvent(skipenemy: boolean);
    procedure SetupEnemy;
    procedure SetupBar(col: TColor);
    procedure Reselect(s, l: integer);
    function CountStars(str: string): integer;
    procedure UpdateVars;
  public
    { Public declarations }
    highorder: integer;
  end;

var
  FormHP: TFormHP;
  hps, hpl, sps, spl, gp1s, gp1l, gp2s, gp2l, el, es: integer;
  hpms, hpml, spms, spml, gp1ms, gp1ml, gp2ms, gp2ml: integer;
  phase: integer;
  dohp, dosp, dogp1, dogp2, doenemy, doenemycase: boolean;
  nomaxhp, nomaxsp, nomaxgp1, nomaxgp2: boolean;
  vars: TStringList;

implementation

{$R *.DFM}

procedure TFormHP.FormCreate(Sender: TObject);
begin
  highorder:=-1;
  PanelMain.left:=8;
  PanelMain.top:=60;
  PanelReady.left:=PanelMain.left;
  PanelReady.top:=PanelMain.top;
  PanelReady.height:=PanelMain.height;
  PanelReady.width:=PanelMain.width;
  EditText.defattributes.name:='Courier New';
  EditText.defattributes.color:=clblack;
  EditText.defattributes.size:=9;
  EditText.defattributes.style:=[];
  vars:=TStringList.create;
end;

procedure TFormHP.FormDestroy(Sender: TObject);
begin
  vars.free;
end;

procedure TFormHP.FormShow(Sender: TObject);
begin
  BtnReset.click;
end;

procedure TFormHP.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormHP.UpdatePhase;
begin
  case phase of
    1: begin
         LabelPhase.caption:=
           'Highlight the current HP value from the text above, then click Next...';
         BtnNo.hide;
         BtnSkip.caption:='&Skip HP';
         BtnSkip.glyph:=s1.picture.bitmap;
         LabelHP.font.color:=clwhite;
         GaugeHP.forecolor:=clmaroon;
       end;
    2: begin
         LabelPhase.caption:=
           'Highlight the Max HP value (if any) from the text above, then click Next...';
         BtnNo.caption:='N&o Max HP';
         BtnNo.show;
         BtnSkip.caption:='&Skip HP';
         BtnSkip.glyph:=s1.picture.bitmap;
         LabelHP.font.color:=clwhite;
         GaugeHP.forecolor:=clred;
       end;
    3: begin
         LabelPhase.caption:=
           'Highlight the current SP value from the text above, then click Next...';
         BtnNo.hide;
         BtnSkip.caption:='&Skip SP';
         BtnSkip.glyph:=s2.picture.bitmap;
         LabelSP.font.color:=clwhite;
         GaugeSP.forecolor:=clnavy;
       end;
    4: begin
         LabelPhase.caption:=
           'Highlight the Max SP value (if any) from the text above, then click Next...';
         BtnNo.caption:='N&o Max SP';
         BtnNo.show;
         BtnSkip.caption:='&Skip SP';
         BtnSkip.glyph:=s2.picture.bitmap;
         LabelSP.font.color:=clwhite;
         GaugeSP.forecolor:=clblue;
       end;
    5: begin
         LabelPhase.caption:=
           'Highlight the current GP1 value from the text above, then click Next...';
         BtnNo.hide;
         BtnSkip.caption:='&Skip GP1';
         BtnSkip.glyph:=s3.picture.bitmap;
         LabelGP1.font.color:=clwhite;
         GaugeGP1.forecolor:=clgreen;
       end;
    6: begin
         LabelPhase.caption:=
           'Highlight the current Max GP1 (if any) value from the text above, then click Next...';
         BtnNo.caption:='N&o Max GP1';
         BtnNo.show;
         BtnSkip.caption:='&Skip GP1';
         BtnSkip.glyph:=s3.picture.bitmap;
         LabelGP1.font.color:=clwhite;
         GaugeGP1.forecolor:=cllime;
       end;
    7: begin
         LabelPhase.caption:=
           'Highlight the current GP2 value from the text above, then click Next...';
         BtnNo.hide;
         BtnSkip.caption:='&Skip GP2';
         BtnSkip.glyph:=s4.picture.bitmap;
         LabelGP2.font.color:=clwhite;
         GaugeGP2.forecolor:=clpurple;
       end;
    8: begin
         LabelPhase.caption:=
           'Highlight the Max GP2 value (if any) from the text above, then click Next...';
         BtnNo.caption:='N&o Max GP2';
         BtnNo.show;
         BtnSkip.caption:='&Skip GP2';
         BtnSkip.glyph:=s4.picture.bitmap;
         LabelGP2.font.color:=clwhite;
         GaugeGP2.forecolor:=clfuchsia;
       end;
    9: begin
         LabelPhase.caption:=
           'Highlight the current Enemy Status value from the text above, then click Next...';
         BtnNo.hide;
         BtnSkip.caption:='&Skip Enemy Status';
         BtnSkip.glyph:=s5.picture.bitmap;
         LabelEnemy.font.color:=clwhite;
         GaugeEnemy.forecolor:=clyellow;
       end;
  end;
end;

procedure TFormHP.SetupBar(col: TColor);
begin
  EditText.selattributes.color:=col;
  EditText.selattributes.style:=[fsbold,fsitalic];
end;

procedure TFormHP.Reselect(s, l: integer);
begin
  EditText.setfocus;
  EditText.selstart:=s;
  EditText.sellength:=l;
end;

procedure TFormHP.BtnNextClick(Sender: TObject);
var
  s, l: integer;
  mess: string;
begin
  BtnReset.enabled:=true;
  s:=EditText.selstart;
  l:=EditText.sellength;
  mess:='HP';
  if l < 1 then begin
    case phase of
      1: mess:='HP';
      2: mess:='Max HP';
      3: mess:='SP';
      4: mess:='Max SP';
      5: mess:='GP1';
      6: mess:='Max GP1';
      7: mess:='GP2';
      8: mess:='Max GP2';
      9: mess:='Enemy Status';
    end;
    messagedlg('You must select the '+mess+' value first',mtinformation,[mbok],0);
    Reselect(s,l);
    exit;
  end;
  case phase of
    1: begin
         hps:=s; hpl:=l;
         GaugeHP.progress:=0;
         SetupBar(clmaroon);
       end;
    2: begin
         if s < hps+hpl then begin
           messagedlg('The Max HP value must come after the current HP value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         if s = hps+hpl then begin
           messagedlg('At least one character must separate the Max HP value '+
             'from the current HP value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         hpms:=s; hpml:=l;
         GaugeHP.progress:=100;
         GaugeHP.forecolor:=clgray;
         SetupBar(clred);
         LabelHP.font.color:=clgray;
         dohp:=true;
       end;
    3: begin
         if s < hpms+hpml then begin
           messagedlg('The SP values must come after the HP values',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         sps:=s; spl:=l;
         GaugeSP.progress:=0;
         SetupBar(clnavy);
       end;
    4: begin
         if s < sps+spl then begin
           messagedlg('The Max SP value must come after the current SP value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         if s = sps+spl then begin
           messagedlg('At least one character must separate the Max SP value '+
             'from the current SP value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         spms:=s; spml:=l;
         GaugeSP.progress:=100;
         GaugeSP.forecolor:=clgray;
         SetupBar(clblue);
         LabelSP.font.color:=clgray;
         dosp:=true;
       end;
    5: begin
         if s < spms+spml then begin
           messagedlg('The GP1 values must come after the SP values',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         gp1s:=s; gp1l:=l;
         GaugeGP1.progress:=0;
         SetupBar(clgreen);
       end;
    6: begin
         if s < gp1s+gp1l then begin
           messagedlg('The Max GP1 value must come after the current GP value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         if s = gp1s+gp1l then begin
           messagedlg('At least one character must separate the Max GP1 value '+
             'from the current GP1 value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         gp1ms:=s; gp1ml:=l;
         GaugeGP1.progress:=100;
         GaugeGP1.forecolor:=clgray;
         SetupBar(cllime);
         LabelGP1.font.color:=clgray;
         dogp1:=true;
       end;
    7: begin
         if s < gp1ms+gp1ml then begin
           messagedlg('The GP2 values must come after the GP1 values',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         gp2s:=s; gp2l:=l;
         GaugeGP2.progress:=0;
         SetupBar(clpurple);
       end;
    8: begin
         if s < gp2s+gp2l then begin
           messagedlg('The Max GP2 value must come after the current GP2 value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         if s = gp2s+gp2l then begin
           messagedlg('At least one character must separate the Max GP2 value '+
             'from the current GP2 value',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         gp2ms:=s; gp2ml:=l;
         GaugeGP2.progress:=100;
         GaugeGP2.forecolor:=clgray;
         SetupBar(clfuchsia);
         LabelGP2.font.color:=clgray;
         dogp2:=true;
       end;
    9: begin
         if s < gp2ms+gp2ml then begin
           messagedlg('The Enemy Status value must come after the GP2 values',
             mtinformation,[mbok],0);
           Reselect(s,l);
           exit;
         end;
         es:=s; el:=l;
         GaugeEnemy.progress:=100;
         GaugeEnemy.forecolor:=clgray;
         SetupBar(clolive);
         doenemy:=true;
       end;
  end;
  if phase = 9 then begin
    Timer.enabled:=false;
    BtnDummy.enabled:=true;
    BtnMove.enabled:=true;
    BtnMove.setfocus;
    BtnNext.enabled:=false;
    BtnSkip.enabled:=false;
    LabelEnemy.font.color:=clgray;
    SetupEvent(false);
    exit;
  end;
  inc(phase);
  UpdatePhase;
  EditText.selstart:=s+l;
  EditText.sellength:=0;
  EditText.setfocus;
  EditText.selstart:=s+l;
  EditText.sellength:=0;
end;

procedure TFormHP.BtnResetClick(Sender: TObject);
begin
  phase:=1;
  hps:=0; hpl:=0; hpms:=0; hpml:=0; sps:=0; spl:=0; spms:=0; spml:=0; el:=0;
  gp1s:=0; gp1l:=0; gp1ms:=0; gp1ml:=0; gp2s:=0; gp2l:=0; gp2ms:=0; gp2ml:=0; es:=0;
  GaugeHP.progress:=0;
  GaugeSP.progress:=0;
  GaugeGP1.progress:=0;
  GaugeGP2.progress:=0;
  GaugeEnemy.progress:=0;
  dohp:=false; dosp:=false; dogp1:=false; dogp2:=false; doenemy:=false;
  nomaxhp:=false; nomaxsp:=false; nomaxgp1:=false; nomaxgp2:=false;
  LabelHP.font.color:=clblack;
  LabelSP.font.color:=clblack;
  LabelGP1.font.color:=clblack;
  LabelGP2.font.color:=clblack;
  LabelEnemy.font.color:=clblack;
  UpdatePhase;
  Timer.enabled:=true;
  if EditText.visible then begin
    EditText.selectall;
    EditText.selattributes.color:=clblack;
    EditText.selattributes.style:=[];
    Edittext.setfocus;
    Edittext.selstart:=0;
    Edittext.sellength:=0;
  end
  else begin
    EditPaste.setfocus;
    EditPaste.selstart:=0;
    EditPaste.sellength:=0;
  end;
  EditEvent.text:='';
  EditVars.text:='';
  EditEnemy.text:='';
  BtnNext.enabled:=true;
  BtnSkip.enabled:=true;
  BtnOK.enabled:=false;
  BtnReset.enabled:=false;
  BtnDummy.enabled:=false;
  BtnMove.enabled:=false;
  EditText.color:=clwindow;
  EditEvent.color:=clsilver;
  EditEvent.tabstop:=false;
  vars.clear;
  PanelCover.top:=103;
  PanelCover.left:=1;
  PanelCover.show;
  DoEnemyCase:=false;
end;

procedure TFormHP.TimerTimer(Sender: TObject);
var g: TGauge;
begin
  case phase of
    1: g:=GaugeHP;
    2: g:=GaugeHP;
    3: g:=GaugeSP;
    4: g:=GaugeSP;
    5: g:=GaugeGP1;
    6: g:=GaugeGP1;
    7: g:=GaugeGP2;
    8: g:=GaugeGP2;
    9: g:=GaugeEnemy;
    else raise Exception.CreateFmt('TFormHP.TimerTimer: invalid phase value: %d', [phase]);
  end;
  if g.progress = 0 then g.progress:=100 else g.progress:=0;
end;

procedure TFormHP.EditTextKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnNext.click;
  end;
end;

procedure TFormHP.BtnSkipClick(Sender: TObject);
begin
  BtnReset.enabled:=true;
  if (phase > 8) and (not dohp) and (not dosp) and (not dogp1) and (not doenemy) then begin
    messagedlg('You must have at least one value defined.  Click Start Over to redo',
      mtinformation,[mbok],0);
    exit;
  end;
  case phase of
    1: begin
         dohp:=false;
         hps:=0;
         inc(phase,2);
         GaugeHP.progress:=0;
         LabelHP.font.color:=clgray;
       end;
    2: begin
         dohp:=false;
         EditText.selstart:=hps;
         EditText.sellength:=hpl;
         EditText.selattributes.color:=clblack;
         EditText.selattributes.style:=[];
         hps:=0;
         inc(phase);
         GaugeHP.progress:=0;
         LabelHP.font.color:=clgray;
       end;
    3: begin
         dosp:=false;
         sps:=0;
         inc(phase,2);
         GaugeSP.progress:=0;
         LabelSP.font.color:=clgray;
       end;
    4: begin
         dosp:=false;
         EditText.selstart:=sps;
         EditText.sellength:=spl;
         EditText.selattributes.color:=clblack;
         EditText.selattributes.style:=[];
         EditText.setfocus;
         sps:=0;
         inc(phase);
         GaugeSP.progress:=0;
         LabelSP.font.color:=clgray;
       end;
    5: begin
         dogp1:=false;
         gp1s:=0;
         inc(phase,2);
         GaugeGP1.progress:=0;
         LabelGP1.font.color:=clgray;
       end;
    6: begin
         dogp1:=false;
         EditText.selstart:=gp1s;
         EditText.sellength:=gp1l;
         EditText.selattributes.color:=clblack;
         EditText.selattributes.style:=[];
         gp1s:=0;
         inc(phase);
         GaugeGP1.progress:=0;
         LabelGP1.font.color:=clgray;
       end;
    7: begin
         dogp2:=false;
         gp2s:=0;
         inc(phase,2);
         GaugeGP2.progress:=0;
         LabelGP2.font.color:=clgray;
       end;
    8: begin
         dogp2:=false;
         EditText.selstart:=gp2s;
         EditText.sellength:=gp2l;
         EditText.selattributes.color:=clblack;
         EditText.selattributes.style:=[];
         gp2s:=0;
         inc(phase);
         GaugeGP2.progress:=0;
         LabelGP2.font.color:=clgray;
       end;
    9: begin
         doenemy:=false;
         es:=0;
         inc(phase);
         GaugeEnemy.progress:=0;
         LabelEnemy.font.color:=clgray;
       end;
  end;
  case phase of
    3: begin
            EditText.setfocus;
            EditText.selstart:=0;
            EditText.sellength:=0;
          end;
    5: begin
            EditText.setfocus;
            if dohp then EditText.selstart:=hpms+hpml
            else EditText.selstart:=0;
            EditText.sellength:=0;
          end;
    7: begin
            EditText.setfocus;
            if dosp then EditText.selstart:=spms+spml
            else if dohp then EditText.selstart:=hpms+hpml
            else EditText.selstart:=0;
            EditText.sellength:=0;
          end;
    9: begin
            EditText.setfocus;
            if dogp1 then EditText.selstart:=gp1ms+gp1ml
            else if dosp then EditText.selstart:=spms+spml
            else if dohp then EditText.selstart:=hpms+hpml
            else EditText.selstart:=0;
            EditText.sellength:=0;
          end;
  end;
  if phase <= 9 then UpdatePhase
  else begin
    if (not dohp) and (not dosp) and (not dogp1) and (not dogp2) and (not doenemy) then begin
      messagedlg('You must have at least one value defined.  Click Start Over to redo',
        mtinformation,[mbok],0);
      exit;
    end;
    Timer.enabled:=false;
    BtnDummy.enabled:=true;
    BtnMove.enabled:=true;
    BtnMove.setfocus;
    BtnNext.enabled:=false;
    BtnSkip.enabled:=false;
    LabelHP.font.color:=clgray;
    LabelSP.font.color:=clgray;
    LabelGP1.font.color:=clgray;
    LabelGP2.font.color:=clgray;
    LabelEnemy.font.color:=clgray;
    SetupEvent(false);
  end;
end;

procedure TFormHP.BtnReadyClick(Sender: TObject);
begin
  PanelReady.hide;
  PanelMain.show;
  if pos('*',EditPaste.text) > 0 then begin
    messagedlg('You can''t have any *''s in the event text',mtinformation,[mbok],0);
    EditPaste.selectall;
    EditPaste.setfocus;
    exit;
  end;
  EditText.text:=EditPaste.text;
  EditText.selectall;
  EditText.selattributes.name:='Courier New';
  EditText.selattributes.color:=clblack;
  EditText.selattributes.size:=9;
  EditText.selattributes.style:=[];
  EditPaste.hide;
  EditText.show;
  EditText.setfocus;
  EditText.selstart:=0;
  EditText.sellength:=0;
end;

procedure TFormHP.SetupEvent(skipenemy: boolean);
var
  work, event: string;
  p: integer;
begin
  event:='';
  vars.clear;
  work:=EditText.text;

{
memo1.lines.add(inttostr(hps)+','+inttostr(hpl));
memo1.lines.add(inttostr(hpms)+','+inttostr(hpml));
memo1.lines.add(inttostr(sps)+','+inttostr(spl));
memo1.lines.add(inttostr(spms)+','+inttostr(spml));
memo1.lines.add(inttostr(gp1s)+','+inttostr(gp1l));
memo1.lines.add(inttostr(gp1ms)+','+inttostr(gp1ml));
memo1.lines.add(inttostr(gp2s)+','+inttostr(gp2l));
memo1.lines.add(inttostr(gp2ms)+','+inttostr(gp2ml));
memo1.lines.add(inttostr(es)+','+inttostr(el));

         1111111111222222222233333333334444444444
1234567890123456789012345678901234567890123456789
HP:50/100 SP:40/100 NP:65/140 Worth:75/100 Blah
}

  if dohp then begin
    p:=1;
    event:=event+copy(work,p,hps)+'*';
    vars.add('@HP');
    if nomaxhp then begin
      hpms:=hps;
      hpml:=hpl;
    end
    else begin
      p:=hps+hpl+1;
      event:=event+copy(work,p,hpms-p+1)+'*';
      vars.add('@MAXHP');
    end;
    if (not dosp) and (not dogp1) and (not dogp2) and (not doenemy) then
      event:=event+copy(work,hpms+hpml+1,9999);
  end;
  if dosp then begin
    if dohp then p:=hpms+hpml+1 else p:=1;
    event:=event+copy(work,p,sps-p+1)+'*';
    vars.add('@SP');
    if nomaxsp then begin
      spms:=sps;
      spml:=spl;
    end
    else begin
      p:=sps+spl+1;
      event:=event+copy(work,p,spms-p+1)+'*';
      vars.add('@MAXSP');
    end;
    if (not dogp1) and (not dogp2) and (not doenemy) then
      event:=event+copy(work,spms+spml+1,9999);
  end;
  if dogp1 then begin
    if dosp then p:=spms+spml+1
    else begin
      if dohp then p:=hpms+hpml+1
      else p:=1;
    end;
    event:=event+copy(work,p,gp1s-p+1)+'*';
    vars.add('@GP1');
    if nomaxgp1 then begin
      gp1ms:=gp1s;
      gp1ml:=gp1l;
    end
    else begin
      p:=gp1s+gp1l+1;
      event:=event+copy(work,p,gp1ms-p+1)+'*';
      vars.add('@MAXGP1');
    end;
    if (not dogp2) and (not doenemy) then event:=event+copy(work,gp1ms+gp1ml+1,9999);
  end;
  if dogp2 then begin
    if dogp1 then p:=gp1ms+gp1ml+1
    else begin
      if dosp then p:=spms+spml+1
      else begin
        if dohp then p:=hpms+hpml+1
        else p:=1;
      end;
    end;
    event:=event+copy(work,p,gp2s-p+1)+'*';
    vars.add('@GP2');
    if nomaxgp2 then begin
      gp2ms:=gp2s;
      gp2ml:=gp2l;
    end
    else begin
      p:=gp2s+gp2l+1;
      event:=event+copy(work,p,gp2ms-p+1)+'*';
      vars.add('@MAXGP2');
    end;
    if not doenemy then event:=event+copy(work,gp2ms+gp2ml+1,9999);
  end;
  if doenemy then begin
    if dogp2 then p:=gp2ms+gp2ml+1
    else begin
      if dogp1 then p:=gp1ms+gp1ml+1
      else begin
        if dosp then p:=spms+spml+1
        else begin
          if dohp then p:=hpms+hpml+1
          else p:=1;
        end;
      end;
    end;
    event:=event+copy(work,p,es-p+1)+'*';
    event:=event+copy(work,es+el+1,9999);
    vars.add('@ENEMY');
  end;
  if not skipenemy then SetupEnemy;
  EditEvent.text:=event;
  EditEvent.color:=clwindow;
  EditEvent.tabstop:=true;
  EditEvent.setfocus;
  EditEvent.selstart:=0;
  EditEvent.sellength:=0;
  BtnMove.setfocus;
  phase:=1;
  UpdateVars;
  PanelCover.hide;
end;

procedure TFormHP.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if ((activecontrol = EditText) or (activecontrol = EditEvent)) and
    (key = vk_Down) then key:=0;
end;

procedure TFormHP.EditPasteKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    if BtnReady.enabled then BtnReady.click;
  end;
end;

procedure TFormHP.EditPasteChange(Sender: TObject);
begin
  if EditPaste.text = '' then BtnReady.enabled:=false else BtnReady.enabled:=true;
end;

procedure TFormHP.EditEventKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnDummy.click;
  end;
end;

function TFormHP.CountStars(str: string): integer;
var i: integer;
begin
  result:=0;
  i:=pos('*',str);
  while i > 0 do begin
    str:=copy(str,1,i-1)+copy(str,i+1,9999);
    inc(result);
    i:=pos('*',str);
  end;
end;

procedure TFormHP.BtnDummyClick(Sender: TObject);
var
  s, l: integer;
  work, pre, sel, pro: string;
  didfail: boolean;
begin
  s:=EditEvent.selstart;
  l:=EditEvent.sellength;
  if l < 1 then begin
    messagedlg('You need to select text to replace with a dummy variable',
      mtinformation,[mbok],0);
    Editevent.setfocus;
    Editevent.selstart:=s;
    Editevent.sellength:=l;
    exit;
  end;
  if CountStars(EditEvent.text) >= 25 then begin
    messagedlg('You can only have a max of 25 *''s in the Event Text',
      mtinformation,[mbok],0);
    BtnMove.setfocus;
    exit;
  end;
  work:=EditEvent.text;
  pre:=copy(work,1,s);
  sel:=copy(work,s+1,l);
  pro:=copy(work,s+1+l,9999);
  didfail:=false;
  if pos('*',sel) > 0 then didfail:=true
  else if copy(pre,length(pre),1) = '*' then didfail:=true
  else if copy(pro,1,1) = '*' then didfail:=true;
  if didfail then begin
    messagedlg('Dummy variables must be placed at least one character from '+
      'any *''s in the Event Text',mtinformation,[mbok],0);
    EditEvent.setfocus;
    EditEvent.selstart:=s;
    EditEvent.sellength:=l;
    exit;
  end;
  vars.insert(CountStars(pre),'<@>');
  EditEvent.text:=pre+'*'+pro;
  BtnMove.setfocus;
  BtnUndoDummy.enabled:=true;
  UpdateVars;
end;

procedure TFormHP.BtnUndoDummyClick(Sender: TObject);
begin
  BtnUndoDummy.enabled:=false;
  SetupEvent(true);
end;

procedure TFormHP.UpdateVars;
var
  i: integer;
  work: string;
begin
  work:='';
  for i:=0 to vars.count-1 do work:=work+','+vars[i];
  if copy(work,1,1) = ',' then delete(work,1,1);
  EditVars.text:=work;
end;

procedure TFormHP.SetupEnemy;
var
  i: integer;
  work: string;
  strs: TStringList;
  form: TFormEnemySetup;
  linked, actual: boolean;
begin
  if not doenemy then exit;
  strs:=TStringList.create;
  form:=TFormEnemySetup.create(self);
  form.showmodal;
  linked:=form.linked;
  actual:=form.actual;
  strs.assign(form.List1.items);
  form.free;
  if actual then begin
    EditEnemy.text:='<no case statement being used>';
    exit;
  end;
  doenemycase:=true;
  if not linked then begin
    BtnReset.click;
    abort;
  end;
  work:='@(@AA';
  for i:=0 to strs.count-1 do work:=work+','+strs[i];
  work:=work+')';
  EditEnemy.text:=work;
  strs.free;
  for i:=0 to vars.count-1 do begin
    if vars[i] = '@ENEMY' then begin
      vars[i]:='@AA';
      break;
    end;
  end;
  UpdateVars;
end;

procedure TFormHP.BtnMoveClick(Sender: TObject);
begin
  messagedlg('The HP Bar event is now ready.  Please review and then click Accept or Cancel',
    mtinformation,[mbok],0);
  BtnOK.enabled:=true;
  BtnOK.setfocus;
end;

procedure TFormHP.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('hp_bar_assistant');
end;

procedure TFormHP.BtnPasteClick(Sender: TObject);
begin
  EditPaste.text:='';
  EditPaste.PasteFromClipboard;
end;

procedure TFormHP.BtnOKClick(Sender: TObject);
var
  didfind: boolean;
  work: string;
  i: integer;
  coms: TStringList;
begin
  work:='';
//  mr:=messagedlg('Do you wish the MUD''S HP info to be filtered'+#13+
//    'and  not show up on the main screen?'+#13+
//    '["Yes" is recommended]',mtconfirmation,[mbyes,mbno,mbcancel],0);
//  if mr = mrcancel then exit;
  for i:=0 to vars.count-1 do begin
    if vars[i] = '<@>' then work:=work+','+vars[i]
    else work:=work+','+copy(vars[i],2,9999);
  end;
  if copy(work,1,1) = ',' then delete(work,1,1);
  with Datawell.TableEvents do begin
    filter := format('(Master = %s) and (FGROUP = "BASE")', [Datawell.masterindex]);
    filtered := true;
    active:=true;
    first;
    didfind:=false;
    while not EOF do begin
      if fieldbyname('FEvent').asstring = EditEvent.text then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if didfind then begin
      if messagedlg('Event already exists for this group.  Replace?',
        mtconfirmation,[mbyes,mbno],0) = mrno then begin
        BtnOK.setfocus;
        exit;
      end;
      edit;
    end;
    append;
    fieldbyname('Master').asinteger:=Datawell.masterinteger;
    fieldbyname('FGroup').asstring:='BASE';
    fieldbyname('FEvent').asstring:=EditEvent.text;
    fieldbyname('FAction').asstring:=work;
    fieldbyname('Sound').asstring:='';
    fieldbyname('Soundfile').asstring:='';
    fieldbyname('FColor').asinteger:=0;
    fieldbyname('BColor').asinteger:=0;
    fieldbyname('CaseSensitive').asboolean:=false;
//    fieldbyname('Filtered').asboolean:=(mr = mryes);
    fieldbyname('Filtered').asboolean:=false;
    fieldbyname('FRetain').asboolean:=false;
    fieldbyname('Terminal').asboolean:=false;
    fieldbyname('FOrder').asinteger:=highorder;
    if doenemycase then begin
      coms:=TStringList.create;
      coms.add(';process @ENEMY='+EditEnemy.text);
      fieldbyname('Script').assign(coms);
      coms.free;
    end;
    post;
  end;
  close;
  messagedlg('The HP Bar has been successfully added as an event to the BASE group.'+#13+
    'Please see the events screen to make any changes',
    mtinformation,[mbok],0);
end;

procedure TFormHP.BtnNoClick(Sender: TObject);
var s, l: integer;
begin
  s:=EditText.selstart;
  l:=EditText.sellength;
  case phase of
    2: begin
         nomaxhp:=true;
         dohp:=true;
       end;
    4: begin
         nomaxsp:=true;
         dosp:=true;
       end;
    6: begin
         nomaxgp1:=true;
         dogp1:=true;
       end;
    8: begin
         nomaxgp1:=true;
         dogp2:=true;
       end;
  end;
  inc(phase);
  UpdatePhase;
  EditText.selstart:=s+l;
  EditText.sellength:=0;
  EditText.setfocus;
  EditText.selstart:=s+l;
  EditText.sellength:=0;
end;

end.
